package com.dataart.productshop.somepackageforspringstopfuckmybrain;

import com.dataart.productshop.Constants;
import com.dataart.productshop.file.UploadPicture;
import com.dataart.productshop.file.utils.SavePicture;
import com.dataart.productshop.service.FileInfoManager;
import com.dataart.productshop.service.PictureManager;
import com.dataart.productshop.service.TypeManager;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.multipart.commons.CommonsMultipartFile;

@Controller
@RequestMapping("/pictures")
public class UploadPictureController {

	public static final String MAIN_PICT_UPLOAD_VIEW = "pictureUpload";
	public static final String SAMPLE_FOLDER = "sample";

	@Value(Constants.PROP_PICT_FILE_PATH)
	private String folderPath;

	@Autowired
	private FileInfoManager fileInfoManager;

	@Autowired
	private TypeManager typeManager;

	@Autowired
	private PictureManager pictureManager;

	private CommonsMultipartFile pictureFile;

	@RequestMapping(method = RequestMethod.GET)
	public String getUploadForm(Model model) {
		model.addAttribute("types", typeManager.getAll());
		model.addAttribute("uploadPicture", new UploadPicture());
		return MAIN_PICT_UPLOAD_VIEW;
	}

	@RequestMapping(method = RequestMethod.POST)
	public String savePicture(
			@ModelAttribute("uploadPicture") UploadPicture pictureFile,
			Model model) {
		new SavePicture().savePictures(pictureFile.getPictureFile(),
				folderPath);
		return MAIN_PICT_UPLOAD_VIEW;
	}

}
